package com.hrsoft.edi.stub;

import cn.hutool.core.io.FileUtil;
import com.hrsoft.edi.stub.consant.AppInfo;
import com.hrsoft.edi.stub.request.InventoryReportRequest;
import com.hrsoft.edi.util.BaseContextHandler;
import com.qimen.api.DefaultQimenClient;
import com.qimen.api.QimenClient;
import com.qimen.api.response.InventoryReportResponse;
import com.taobao.api.ApiException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public class Test2
{
    public static void main(String[] args)
    {
        List<String> erpLines =
            FileUtil.readLines("/Users/dzl/IdeaProjects/qm-stub3.0/data/1.txt", "utf8");

        List<String> wmsLines =
            FileUtil.readLines("/Users/dzl/IdeaProjects/qm-stub3.0/data/wms-date.txt", "utf8");

        Map<String, WmsProductDate> dateMap = new HashMap<>();
        for (String wmsLine : wmsLines)
        {
            String[] strings = wmsLine.split("\\s+");
            if (strings.length == 3)
            {
                WmsProductDate wmsProductDate = new WmsProductDate();
                wmsProductDate.setItemKey(strings[0]);
                wmsProductDate.setProductDate(strings[1]);
                wmsProductDate.setExpireDate(strings[2]);
                dateMap.put(strings[0], wmsProductDate);
            }
        }

        List<ErpLine> erpLineList = new ArrayList<>();
        for (String erpLine : erpLines)
        {
            String[] strings = erpLine.split("\\s+");
            ErpLine erpLine1 = new ErpLine();
            erpLine1.setItemKey(strings[1]);
            erpLine1.setQty(Integer.parseInt(strings[0]));
            erpLine1.setItemCode(strings[2]);
            erpLineList.add(erpLine1);
        }

        Map<String, List<ErpLine>> map =
            erpLineList.stream().collect(Collectors.groupingBy(employee -> employee.getItemKey()));

        // 找出每个key反馈的总数
        Map<String, WmsProductDate> mapQty = new HashMap<>();
        map.entrySet().stream().forEach(stringListEntry -> {
            Integer totalQty = stringListEntry.getValue().stream().mapToInt(employee -> employee.getQty()).sum();
            WmsProductDate wmsProductDate = new WmsProductDate();
            wmsProductDate.setItemKey(stringListEntry.getValue().get(0).getItemKey());
            wmsProductDate.setItemCode(stringListEntry.getValue().get(0).getItemCode());
            if (dateMap.containsKey(wmsProductDate.getItemKey()))
            {
                WmsProductDate wmsProductDate1 = dateMap.get(wmsProductDate.getItemKey());
                wmsProductDate.setProductDate(wmsProductDate1.getProductDate());
                wmsProductDate.setExpireDate(wmsProductDate1.getExpireDate());
            }
            wmsProductDate.setQty(totalQty);
            mapQty.put(stringListEntry.getKey(), wmsProductDate);
        });

        System.out.println(mapQty.entrySet().size());

        int pageSize = 1;

        for (Map.Entry<String, WmsProductDate> stringIntegerEntry : mapQty.entrySet())
        {
            if (pageSize != 1)
            {
                InventoryReportRequest inventoryReportRequest = new InventoryReportRequest();
                inventoryReportRequest.setCheckOrderCode("202101122001");
                inventoryReportRequest.setCheckTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                inventoryReportRequest.setPageSize(1L);
                inventoryReportRequest.setCurrentPage(Long.valueOf(pageSize));
                inventoryReportRequest.setTotalPage(Long.valueOf(mapQty.keySet().size()));
                inventoryReportRequest.setOwnerCode("SZKT");
                inventoryReportRequest.setWarehouseCode("HD02");
                List<InventoryReportRequest.Item> items = new ArrayList<>();
                InventoryReportRequest.Item item = new InventoryReportRequest.Item();
                item.setItemCode(stringIntegerEntry.getValue().getItemCode());

                item.setQuantity(stringIntegerEntry.getValue().getQty().longValue()*-1);
                item.setInventoryType("ZP");
                if (null != stringIntegerEntry.getValue().getProductDate())
                {
                    item.setExpireDate(stringIntegerEntry.getValue().getExpireDate());
                    item.setProductDate(stringIntegerEntry.getValue().getProductDate());
                }
                items.add(item);

                inventoryReportRequest.setOutBizCode(stringIntegerEntry.getKey() + "-202101122001");

                inventoryReportRequest.setItems(items);

                QimenClient client = new DefaultQimenClient(AppInfo.URL, AppInfo.APP_KEY, AppInfo.SECRET);

                InventoryReportResponse response = null;
                try
                {
                    response = client.execute(inventoryReportRequest);
                }
                catch (ApiException e)
                {
                    e.printStackTrace();
                }
                log.info("url:{} outBizCode:{} response:{}",
                    BaseContextHandler.get("url"),
                    inventoryReportRequest.getOutBizCode(),
                    response.getFlag());
            }

            pageSize++;
        }

    }

    @Data
    public static class ErpLine
    {
        String itemKey;

        Integer Qty;

        String itemCode;

        @Override
        public boolean equals(Object o)
        {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            ErpLine erpLine = (ErpLine)o;
            return itemKey.equals(erpLine.itemKey);
        }

        @Override
        public int hashCode()
        {
            return Objects.hash(itemKey);
        }
    }

    @Data
    public static class WmsProductDate
    {
        String itemKey;

        String itemCode;

        String productDate;

        String expireDate;

        Integer qty;

        @Override
        public boolean equals(Object o)
        {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            ErpLine erpLine = (ErpLine)o;
            return itemKey.equals(erpLine.itemKey);
        }

        @Override
        public int hashCode()
        {
            return Objects.hash(itemKey);
        }
    }
}
